package com.shd.edanpei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.bean.MessageModel;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.HomeSearchPresenter;
import com.shd.edanpei.presenter.contract.HomeSearchContract;
import com.shd.edanpei.ui.adapter.DynamicsAdapter;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/12/26.
 * yang-wen-wen
 */

public class HomeSearchActivity extends BaseActivity<HomeSearchPresenter> implements HomeSearchContract.HomeSearchIView {


    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private String str;

    private DynamicsAdapter mAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_home_search;
    }

    @Override
    protected HomeSearchPresenter bindPresenter() {
        return new HomeSearchPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new DynamicsAdapter(this);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initListener() {
        etSearch.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (etSearch.getText().toString().isEmpty()) {
                        return true;
                    } else {
                        String str = etSearch.getText().toString();
                        mPresenter.refresh(str);
                    }
                }
                return false;
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore(str);
            }
        }, recyclerView);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageModel item = mAdapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constant.DATA, mAdapter.getItem(position));
                Intent intent = new Intent(HomeSearchActivity.this, DynamicsDetailsActivity.class);
                intent.putExtras(bundle);
                ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(HomeSearchActivity.this, mAdapter.getViewByPosition(position, R.id.picture), "picture");
                ActivityCompat.startActivity(HomeSearchActivity.this, intent, compat.toBundle());
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
        return false;
    }

    @OnClick(R.id.cancel)
    public void back() {
        onBackPressed();
    }

    @Override
    public DynamicsAdapter getAdapter() {
        return mAdapter;
    }
}
